<?php
namespace app\admin\controller;
use app\admin\model\Question as QuestionModel;
use think\Image;
use think\Request;

class Question extends Common{
    protected $question;
    protected $request;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->question=new QuestionModel();
        $this->request=Request::instance();
    }
    public function index(){
        $keyword=input('keyword','');
        $where='';
        if(!empty($keyword)){
            $where="q_title like '%$keyword%'";
        }
        $question=$this->question->pageData($where,$keyword);
        $this->assign('question',$question);
        return $this->fetch();

    }
    public function edite(){
        $id=input('q_id');
        if($this->request->isGet()){
            $data=$this->question->getOne(['q_id'=>$id]);
            $this->assign('data',$data);
            return $this->fetch();
        }else{
            $data=$this->request->post();
            $res=$this->question->saveData(['q_id'=>$id],$data);
            if($res){
                $this->success('修改用户文章成功','question/index');

            }else{
                $this->error('修改用户文章失败','question/edite?q_id='.$id);
            }
        }
    }
    public function adopt(){
        $id=input('q_id');
        $res=$this->question->saveData(['q_id'=>$id],['audit'=>1]);
        if ($res) {
            return json([
                'status' => 1,
                'msg' => '审核成功'
            ]);
        }else{
            return json([
                'status' => 0,
                'msg' => '审核失败'
            ]);
        }
    }
    public function nopass(){
        $id=input('q_id');
        $res=$this->question->saveData(['q_id'=>$id],['audit'=>0]);
        if ($res) {
            return json([
                'status' => 1,
                'msg' => '审核成功'
            ]);
        }else{
            return json([
                'status' => 0,
                'msg' => '审核失败'
            ]);
        }
    }
    public function uploadImg()
    {

        $file = $this->request->file('file_data');
        //原图保存路径
        $path = ROOT_PATH . 'public' . DS . 'uploads' . DS . 'question';
        $info = $file->move($path);
        //缩略图名称
        $thumb_name = 'thumb_' . $info->getFilename();
        //缩略图保存路径和文件名称
        $thumb_path = ROOT_PATH . 'public' . DS . 'uploads' . DS . 'question' . DS . 'thumb' . DS . $thumb_name;
        if ($info) {
            //缩略图
            $image = Image::open($path.DS.$info->getSaveName());
            $image->thumb(200, 200, Image::THUMB_CENTER)->save($thumb_path);
            return json([
                'status' => 1,
                'path' => $info->getSaveName(),
                'thumb' => 'thumb' . DS . $thumb_name
            ]);
        } else {
            return json([
                'status' => 0,
                'err' => $file->getError()
            ]);
        }
    }
}